package cn.bdqn.services.dept.controller.sysuserinfo;

import cn.bdqn.model.pojo.Menu;
import cn.bdqn.model.pojo.Role;
import cn.bdqn.model.pojo.SysUserinfo;
import cn.bdqn.model.util.GrantRole;
import cn.bdqn.model.util.LoginMap;
import cn.bdqn.model.util.MenuUtil;
import cn.bdqn.model.util.Meta;
import cn.bdqn.services.dept.service.SysUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 徐凤年
 * @Date 2022/3/29 15:18
 * @Version 1.0
 */
@RestController
@CrossOrigin
public class SysUserInfoController {

    @Autowired
    private SysUserinfoService service;
    @RequestMapping("/login")
    public LoginMap login(@RequestBody SysUserinfo sysUserinfo){
        System.out.println("----------login---------");
        SysUserinfo user1 = service.login(sysUserinfo.getUsername(),sysUserinfo.getPassword());
        LoginMap map = new LoginMap();
        if (user1!=null){
            map.setCode(20000);
            map.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
            map.setName(user1.getName());
            map.setToken(user1.getUserId()+"");
            System.out.println(user1.getUserId());
            System.out.println(user1.getName());

            List<Role> roles1 = service.getRoleListUser(user1.getUserId());
            if (roles1!=null){
                String[] roles2 = new String[roles1.size()];
                for (int i=0;i<roles2.length;i++){
                    roles2[i] =roles1.get(i).getRname();
                }
                map.setRoles(roles2);
            }
            else {
                map.setCode(60204);
                map.setMessage("用户没有角色信息 无法登陆 请联系管理员");
            }
        }else {
            map.setCode(60204);
            map.setMessage("账号或密码错误！");
        }
        return map;
    }

    @RequestMapping("/getInfo")
    public LoginMap getInfo(int token) {
        System.out.println("------info------");
        System.out.println("id="+token);
        LoginMap msg = new LoginMap();
        SysUserinfo user1 = service.selectId(token);
        List<Role> roles =service.getRoleListUser(token);
        if(roles!=null){
            String[] roles1 = new String[roles.size()];
            for(int i = 0;i<roles1.length;i++){
                roles1[i] = roles.get(i).getRname();
            }
            msg.setRoles(roles1);
        }else{
            msg.setCode(60204);
            msg.setMessage("该用户没有角色，不能登录！请联系管理员1");
        }

        msg.setCode(20000);
        msg.setAvatar("https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        msg.setName(user1.getName());
        msg.setToken(user1.getUserId()+"");
        return msg;
    }

    @RequestMapping("/Logout")
    public Map<String,Object> Logout(){
        System.out.println("----logout----");
        Map<String,Object> map = new HashMap<>();
        map.put("code",20000);
        map.put("data","success");
        return map;
    }

    @RequestMapping("getMenuRoleList")
    public Map<String,Object> getMenuList(@RequestBody Role roles){
        System.out.println("-----getMenuRoleList----");
        System.out.println(roles);
        List<Menu> menuList =service.getMenRoleList(roles.getRoles());
        List<MenuUtil> menuUtilList =new ArrayList<>();
        MenuUtil mu =null;
        for (Menu m:menuList){
            Meta meta=null;
            System.out.println("m=" + m.getName());
            if (m.getLeval()==1){
                mu=new MenuUtil();
                mu.setPath(m.getPath());
                mu.setAlwaysShow(true);
                mu.setComponent(m.getComponent());
                mu.setName(m.getName());
                mu.setMeta(new Meta(m.getTitle(),m.getIcon()));
                List<MenuUtil> chi = new ArrayList<>();
                for (Menu m1:menuList){
                    MenuUtil mu1=null;
                    if (m1.getParentId()==m.getId()){
                        mu1 = new MenuUtil();
                        mu1.setPath(m1.getPath());
                        mu1.setAlwaysShow(false);
                        mu1.setName(m1.getName());
                        mu1.setComponent(m1.getComponent());
                        mu1.setMeta(new Meta(m1.getTitle(),m1.getIcon()));
                        mu1.setChildren(new ArrayList<>(0));
                        chi.add(mu1);
                    }
                }
                mu.setChildren(chi);
                menuUtilList.add(mu);
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",20000);
        map.put("data","success");
        map.put("menu",menuUtilList);
        return map;
    }

    //授权
    @RequestMapping(value = "grantRole",method = RequestMethod.POST)
    public int gramntRole(@RequestBody GrantRole grantRole){
        System.out.println("-----getGrant------");
        System.out.println("uid=="+grantRole.getUid());
        for (int i:grantRole.getRids()
        ) {
            System.out.println(i);
        }
        if (grantRole!=null){
            return service.grantUser(grantRole.getUid(),grantRole.getRids());
        }
        return 0;
    }

    @RequestMapping(value = "/getRoleListUser/{uid}",method = RequestMethod.GET)
    public List<Role>getRoleListUser(@PathVariable("uid") int uid){
        return service.getRoleListUser(uid);
    }
    }